<template>
  <div class="v-editor-input-number">
    <el-input-number
      v-model="inputvalue"
      :controls="false"
      :disabled="row.readonly || isModelDisabled"
      :min="minNum"
      :max="maxNum"
      @change="handelChange"
    />
  </div>
</template>
<script lang="ts" setup>
import { BusEvent, PropertyKey, PropertyDataType } from "@/constants/Enums";
import { MetaclassType } from "@/constants/MetaclassType";
import { modelService } from "@/modules/model/service";
import { ref, computed, watch, inject, Ref } from "vue";
import { propertyUtil } from "../../PropertyUtil";
import { PropertyRow } from "../../types";
import { JAVA_MAX_INTEGER, JAVA_MIN_INTEGER, MAX_INPUT_NUMBER, MIN_INPUT_NUMBER } from "@/constants/constants";

const isModelDisabled = inject("isModelDisabled") as Ref<boolean>;

const props = defineProps<{
  row: PropertyRow;
  selected?: boolean;
  projectId?: string;
}>();

let inputvalue = ref<number>(props.row.value as number);
const size = 500;
watch(
  () => props.row.value,
  newVal => {
    inputvalue.value = newVal as number;
  }
);

const maxNum = computed(() => {
  if (props.row.key === PropertyKey.DiagramMap_depth) {
    return 10;
  }
  const metaclass = props.row.valueSpecificationInfo?.metaclass;
  if (metaclass) {
    switch (metaclass) {
      case MetaclassType.LiteralUnlimitedNatural:
      case MetaclassType.LiteralInteger:
        return JAVA_MAX_INTEGER;
      case MetaclassType.LiteralReal:
        // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
        return MAX_INPUT_NUMBER;
      default:
        // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
        return MAX_INPUT_NUMBER;
    }
  } else {
    switch (props.row.dataType) {
      case PropertyDataType.INTEGER:
      case PropertyDataType.UNLIMITEDNATURAL:
        return JAVA_MAX_INTEGER;
      case PropertyDataType.REAL:
        // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
        return MAX_INPUT_NUMBER;
      default:
        // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
        return MAX_INPUT_NUMBER;
    }
  }
});

const minNum = computed(() => {
  if (props.row.key === PropertyKey.DiagramMap_depth) {
    return 1;
  }
  if (props.row.key === PropertyKey.ReliabilityProfile_FTAProfile_MAJORITY_VOTE_m) {
    return 1;
  }
  const metaclass = props.row.valueSpecificationInfo?.metaclass;
  if (metaclass) {
    switch (metaclass) {
      case MetaclassType.LiteralUnlimitedNatural:
        return 0;
      case MetaclassType.LiteralInteger:
        return JAVA_MIN_INTEGER;
      case MetaclassType.LiteralReal:
        // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
        return MIN_INPUT_NUMBER;
      default:
        // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
        return MIN_INPUT_NUMBER;
    }
  } else {
    switch (props.row.dataType) {
      case PropertyDataType.INTEGER:
      case PropertyDataType.UNLIMITEDNATURAL:
        return JAVA_MIN_INTEGER;
      case PropertyDataType.REAL:
        // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
        return MIN_INPUT_NUMBER;
      default:
        // eslint-disable-next-line @typescript-eslint/no-loss-of-precision
        return MIN_INPUT_NUMBER;
    }
  }
});

const handelChange = async () => {
  // 若是矩阵行列头部高度则需加限制
  if (props.row.key === PropertyKey.DiagramMatrix_columnHeaderHeight) {
    const matrixHeight = app.activeGraph?.viewModel?.matrixHeight;

    if (matrixHeight && inputvalue.value > matrixHeight) {
      inputvalue.value = matrixHeight;
    } else if (!matrixHeight && inputvalue.value > size) {
      inputvalue.value = size;
    }
  }
  if (props.row.key === PropertyKey.DiagramMatrix_rowHeaderWidth) {
    const matrixWidth = app.activeGraph?.viewModel?.matrixWidth;

    if (matrixWidth && inputvalue.value > matrixWidth) {
      inputvalue.value = matrixWidth;
    } else if (!matrixWidth && inputvalue.value > size) {
      inputvalue.value = size;
    }
  }

  const dto = propertyUtil.getUpdatePropertyDtoByPropertyRow(props.row, props.projectId || app.activeProject!.id);

  dto.value = inputvalue.value;

  if (props.row.key === "87983415192817408") {
    dto.updateMethod = "updateTCPOpaqueActionTCPPort";
  }
  let res = await modelService.updateModelProperty(dto);

  if (res) {
    app.$bus.emit(BusEvent.REFRESH_PROPERTY, props.row.modelId);
  }
};
</script>
<style lang="scss" scoped>
.v-editor-input-number {
  display: flex;
  flex-wrap: nowrap;
  height: auto;
  .el-input-number {
    width: 100%;
  }
  .el-input-number.is-without-controls .el-input__inner {
    padding-right: 0px;
    padding-left: 0px;
  }
}
</style>
